package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.Constants;
import com.qf.utils.StrUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@WebServlet("/user")
public class UserController extends BaseServlet {
    public  void check(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String username = request.getParameter("username");
        UserService userService=new UserServiceImpl();
        boolean check = userService.check(username);
        if(check==true){
            response.getWriter().write(Constants.NOT_HAS_USER);
        }else {
            response.getWriter().write(Constants.HAS_USER);
        }
    }
    public String login(HttpServletRequest request, HttpServletResponse response){
        String username=request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String auto = request.getParameter("auto");
        if(!StrUtils.empty(username)||!StrUtils.empty(password)||!StrUtils.empty(code)){
            request.setAttribute("msg","用户名，密码，验证码不可以为空！");
            return "forward:/login.jsp";
        }
        String sessionCode =(String) request.getSession().getAttribute("code");
        if(!sessionCode.equalsIgnoreCase(code)){
            request.setAttribute("userName",username);
            request.setAttribute("password",password);
            request.setAttribute("msg","验证吗错误!");
            return "forward:/login.jsp";
        }
        UserService userService=new UserServiceImpl();
        User user = userService.login(username, password);
        if(user==null){
            request.setAttribute("userName",username);
            request.setAttribute("password",password);
            request.setAttribute("msg","用户名密码错误");
            return "forward:/login.jsp";
        }
        request.getSession().setAttribute("loginUser",user);
        if("on".equals(auto)){
            Cookie cookie = new Cookie(Constants.AUTO_NAME, user.getUsername());
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*14);
            response.addCookie(cookie);
        }
        return  "redirect:/goods?method=index";
    }
    public String register(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user=new User();
        BeanUtils.populate(user,parameterMap);
        UserService userService=new UserServiceImpl();
        int count = userService.createUser(user);
        if(count>0){
            return "redirect:/registerSuccess.jsp";
        }else {
            return "redirect:/register.jsp";
        }
    }
}
